// pages/mine/address/add/add.js
// var tcity = require("../../../../utils/citys.js");
var app = getApp();
var url = app.globalData.url;
var addressid = 2;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showss: false,
    shows: false,
    show: false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    selectDatas: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    selectDatass: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    index: 0,//选择的下拉列表下标
    provinceid: '',
    cityid: '',
    areaid: '',
    cityname: '请选择市',
    provincename: '请选择省',
    areaname: '请选择区',

     show1:false,
     name:'',
     phone:'',
     map:'',
     address:'',
     state:0,
  },
  name:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  map: function (e) {
    this.setData({
      map: e.detail.value
    })
  },
  address: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  submit:function(e){
    var pages = getCurrentPages();
    // 前一个页面  
    var beforePage = pages[pages.length - 2]; 

    var name = this.data.name;
    console.log(name);
    var phone= this.data.phone;
    var map = this.data.map;
    var address = this.data.address;
    var that = this;
    var province = app.globalData.provinceid;
    var city = app.globalData.cityid;
    var district = app.globalData.areaid;
    var state = this.data.state;
    app.globalData.state = state;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if(name == ""|| phone == ''||address == ""){
      wx.showToast({
        title: '以上选项是必填',
        icon: 'none',
        duration: 2000,
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }else if(phone.length !== 11){
      wx.showToast({
        title: '手机号长度不对',
        icon: 'none',
        duration: 2000,
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1500
      })
    }else{
      const userid = app.globalData.userid;
      wx.request({
        url: url + 'addMemeberUserAddress',
        data: {
          userId: userid,
          consignee: name,
          country: 1,
          province: province,
          city: city,
          district: district,
          address: address,
          mobile: phone,

        },
        header: {
          "Content-Type": "application/json"
        },
        success: function (res) {
          console.log(res.data.object);
          var id = res.data.object;
          var states = app.globalData.state;
          console.log(states);
          wx.request({
            url: url + 'editUserAddressDefault',
            data: {
              userId: userid,
              addressId: id,
              state: states,
            },
            header: {
              "Content-Type": "application/json"
            },
            success: function (res) {
              console.log(res.data.object)
              if (res.data.object == true) {
                wx.showToast({
                  title: '保存成功',
                  icon: '',
                  image: '',
                  duration: 2000,
                  mask: true,
                  success: function (res) {
                    console.log("上一页")
                    beforePage.onLoad();//执行上一个页面的方法
                  },
                })
              } else {
                console.log("保存失败");
              }
              that.setData({
                show1: false,
                name: '',
                phone: '',
                map: '',
                address: '',
                state: 0,
                cityname: '请选择市',
                provincename: '请选择省',
                areaname: '请选择区',
              });
            }
          })

        }
      })
    }
    
  },
  isshow:function(e){
   console.log(e);
   var state = e.currentTarget.dataset.state;
   this.setData({
     show1: true,
     state:1
   })
  },
  noshow: function (e) {
    console.log(e);
    var state = e.currentTarget.dataset.state;
    console.log(state);
    this.setData({
      show1: false,
      state:0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },
  // 点击下拉显示框
  selectTap() {
    this.setData({
      show: !this.data.show
    });
    var that = this;
    //省级
    wx.request({
      url: url+'getAllProvince',
      data: {
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object)
        that.setData({
          provinces: res.data.object
        })
      }
    })
  },
  // 点击下拉列表
  optionTap(e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    app.globalData.provinceid = id;
    let name = e.currentTarget.dataset.name;//获取点击的下拉列表的下标
    this.setData({
      id: id,
      index: index,
      provincename: name,
      show: !this.data.show
    });
  },

  //市级
  // 点击下拉显示框
  selectTaps() {
    this.setData({
      shows: !this.data.shows
    });
    var that = this;
    var provinceid = app.globalData.provinceid;
    //市级
    wx.request({
      url: url+'getCityByProvinceId',
      data: {
        provinceId: provinceid
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object)
        that.setData({
          citys: res.data.object
        })
      }
    })
  },
  // 市级点击下拉列表
  optionTaps(e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    app.globalData.cityid = id;
    let name = e.currentTarget.dataset.name;//获取点击的下拉列表的下标
    this.setData({
      id: id,
      index: index,
      cityname: name,
      shows: !this.data.shows
    });
  },

  //区级
  // 点击下拉显示框
  selectTapss() {
    this.setData({
      showss: !this.data.showss
    });
    var that = this;
    var cityid = app.globalData.cityid;
    //区级
    wx.request({
      url: url+'getAreaByCityId',
      data: {
        cityId: cityid
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object)
        that.setData({
          areas: res.data.object
        })
      }
    })
  },
  // 市级点击下拉列表
  optionTapss(e) {
    var id = e.currentTarget.dataset.id;
    app.globalData.areaid = id;
    var index = e.currentTarget.dataset.index;
    let name = e.currentTarget.dataset.name;//获取点击的下拉列表的下标
    this.setData({
      id: id,
      index: index,
      areaname: name,
      showss: !this.data.showss
    });
  },
})